"""
主要测试 cnode社区的话题相关的功能
主要api包括：
1.  主题首页
"""
# 引入第三方模块
import requests
import business.common as common

def test_topic_page():
    """
    测试主题首页功能
    get请求方法
    url： http://49.233.108.117:3000/api/v1/topics
    :return:
    """
    # 1. 发送get请求
    url = 'http://49.233.108.117:3000/api/v1/topics'
    getdata={
        "page":1,
        "tab":"ask",
        "limit":10,
        "mdrender":"true"
    }
    res = requests.get(url=url,params=getdata)

    # 2.查看状态码
    print('状态码：',res.status_code)
    # 3. 查看json结果
    print(res.json())
    jsondata = res.json()
    data = jsondata['data']
    print(len(data))
    assert len(data)==10
    for i in data:
        assert i['tab']== 'ask'

# test_topic_page()


def test_create_topic():
    """
    创建主题
    :return:
    """
    url=common.get_url()
    create_url = url+'s'
    post_data = {"accesstoken":token,
                 "title":"helloworld",
                 "tab":"ask",
                 "content":"xxxxxxxxxx"}
    res1 = requests.post(url=create_url,data=post_data)
    res2 = requests.post(url=create_url, data=post_data)
    # print(res.status_code)
    # print(res.json())
    assert res1.status_code==200
    res1_topic_id = res1.json()['topic_id']
    res2_topic_id = res2.json()['topic_id']
    assert not res1_topic_id==res2_topic_id

# test_create_topic()

def test_topic_detail():
    """
    主题详情
    :return:
    """
    token = common.get_token()
    post_data = {"accesstoken": token,
                 "title": "helloworld",
                 "tab": "ask",
                 "content": "xxxxxxxxxx"}
    topic_id=common.test_create_topic(post_data)
    url=common.get_url()

    topic_url=url+'/'+topic_id
    get_data={
        'mdrender':'true',
        'accesstoken':token
    }
    res=requests.get(url=topic_url,params=get_data)
    print('主题详情')
    print(res.status_code)
    print(res.json())
    assert res.status_code==200
    jsondata=res.json()
    assert post_data['title']== jsondata['data']['title']
    assert post_data['tab']== jsondata['data']['tab']
# test_topic_detail()


def test_topic_update():
    """
    编辑主题
    :return:
    """
    post_data = {"accesstoken": 'e303f3c0-41e3-4dec-a44f-beb6ff281e6a',
                 "title": "helloworld",
                 "tab": "ask",
                 "content": "xxxxxxxxxx"}
    topic_id=common.test_create_topic(post_data)
    token=common.get_token()
    url=common.get_url()

    update_url=url+'s/update'
    post_update_data={
        'accesstoken':token,
        'topic_id':topic_id,
        'title':'我是彭于晏',
        'tab':'share',
        'content':'谁赞成，谁反对'
    }
    res_update=requests.post(url=update_url,data=post_update_data)
    assert res_update.status_code==200
    # 更新吼的id与原来的id保持一致
    update_jsondata = res_update.json()
    update_topic_id=res_update.json()['topic_id']
    assert topic_id==update_topic_id


        # 1. 获取更新之后的内容
    res = common.get_topic_detail(update_topic_id)
    jsondata = res.json()
        # 断言title字段
    assert post_update_data['title'] == jsondata['data']['title']
    assert post_update_data['tab'] == jsondata['data']['tab']
    assert post_update_data['content'] in jsondata['data']['content']

# def test_topic_update():
#     """
#     编辑主题
#     :return:
#     """
#     post_data = {"accesstoken": common.get_token(),
#                  "title": "helloworld",
#                  "tab": "ask",
#                  "content": "xxxxxxxxxx"}
#     topic_id = common.create_topic(post_data)
#
#     url="http://49.233.108.117:3000/api/v1/topics/update"
#     update_data = {"accesstoken": common.get_token(),
#                  "title": "helloworld1234",
#                  "tab": "ask",
#                  "topic_id":topic_id,
#                  "content": "xxxxxxxxxx1234"}
#
#     res= requests.post(url,data=update_data)
#     print(res.json())
#
#     # 更新后的id 与原来更新之前的id 保持一致。
#     update_topic_id = res.json()['topic_id']
#     assert topic_id == update_topic_id
#
#     # 更新之后的内容进行断言
#     # 1. 获取更新之后的内容
#     res = common.get_topic_detail(update_topic_id)
#     jsondata = res.json()
#     # 断言title字段
#     assert update_data['title'] == jsondata['data']['title']
#     assert update_data['tab'] == jsondata['data']['tab']
#     assert update_data['content'] in jsondata['data']['content']

def test_topic_collect():
    """
    主题收藏
    :return:
    """
    post_data = {"accesstoken": 'e303f3c0-41e3-4dec-a44f-beb6ff281e6a',
                 "title": "helloworld",
                 "tab": "ask",
                 "content": "xxxxxxxxxx"}
    topic_id = common.test_create_topic(post_data)
    token = common.get_token()
    url = common.get_url()

    collect_url=url+'_collect/collect'
    post_data={
        'accesstoken':token,
        'topic_id':topic_id
    }
    res=requests.post(url=collect_url,data=post_data)
    print(res.json())
    # assert topic_id== res.json()['topic_id']

test_topic_collect()

def test_topic_de_collect():
    """
    取消收藏
    :return:
    """
    de_collect_url=url+'_collect/de_collect'
    post_data={
        'accesstoken':token,
        'topic_id':'5f05359b357c334168d7833d'
    }
    res=requests.post(url=de_collect_url,data=post_data)
    print('取消收藏')
    print(res.status_code)
    print(res.json())
# test_topic_de_collect()

def test_topic_from_user():
    """
    用户收藏的主题
    :return:
    """
    post_data = {"accesstoken": 'e303f3c0-41e3-4dec-a44f-beb6ff281e6a',
                 "title": "helloworld",
                 "tab": "ask",
                 "content": "xxxxxxxxxx"}
    topic_id = common.test_create_topic(post_data)
    token = common.get_token()
    url = common.get_url()

    collect_url = url + '_collect/collect'
    post_data = {
        'accesstoken': token,
        'topic_id': topic_id
    }
    res_post = requests.post(url=collect_url, data=post_data)

    topic_collect_url=url+'_collect/test11'
    res_get=requests.get(url=topic_collect_url)
    print(res_get.json())
    # assert topic_id in res_get.json()['topic_id']

test_topic_from_user()

def test_topic_reply():
    """
    话题评论
    :return:
    """
    reply_url=url+'/'+'5f05359b357c334168d7833d'+'/replies'

    post_data={
        'accesstoken':token,
        'content':'你说的对'
    }
    res=requests.post(url=reply_url,data=post_data)
    print('话题评论')
    print(res.status_code)
    print(res.json())
    r=res.json()

# test_topic_reply()
# print(test_topic_reply())


def test_topic_reply_self():
    """
    话题评论
    :return:
    """
    reply_self_url=url+'/'+'5f05359b357c334168d7833d'+'/replies'
    post_data={
        'accesstoken':token,
        'content':'你说的有道理',
        'reply_id':'5f0536fb357c334168d78379'
    }
    res=requests.post(url=reply_self_url,data=post_data)
    print('给自己话题评论')
    print(res.status_code)
    print(res.json())
# test_topic_reply_self()

url1="http://49.233.108.117:3000/api/v1/"
def test_reply_up():
    """
    评论点赞
    :return:
    """
    ups_url=url1+'/reply/'+'5f05359b357c334168d7833d'+'/ups'
    post_data={
        'accesstoken':token
    }
    res=requests.post(url=ups_url,data=post_data)
    print('给评论点赞')
    print(res.status_code)
    print(res.json())
# test_reply_up()

def test_user_info():
    """
    用户详情
    :return:
    """
    user_url=url1+'/user/'+'test11'
    res=requests.get(url=user_url)
    print('用户详情')
    print(res.status_code)
    print(res.json())
# test_user_info()

def test_user_accesstoken():
    """
    验证token的正确性
    :return:
    """
    accesstoken_url=url1+'/accesstoken'
    post_data={
        'accesstoken':token
    }
    res=requests.post(url=accesstoken_url,data=post_data)
    print('验证token的正确性')
    print(res.status_code)
    print(res.json())
# test_user_accesstoken()
